<?php

/**
 * This is the model class for table "tin_tuc".
 *
 * The followings are the available columns in table 'tin_tuc':
 * @property integer $id
 * @property string $tieu_de
 * @property string $noi_dung_trich
 * @property string $hinh_anh
 * @property string $noi_dung
 * @property string $ngay_dang
 * @property string $nguoi_dang
 * @property integer $trang_thai
 * @property integer $so_luong
 * @property integer $uu_tien
 * @property integer $loai_tin
 */
class Tintuc extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TinTuc the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tin_tuc';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tieu_de, noi_dung_trich, hinh_anh, noi_dung, ngay_dang, nguoi_dang, uu_tien, loai_tin', 'required'),
			array('trang_thai, so_luong, uu_tien, loai_tin', 'numerical', 'integerOnly'=>true),
			array('tieu_de, noi_dung_trich', 'length', 'max'=>500),
			array('hinh_anh', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, tieu_de, noi_dung_trich, hinh_anh, noi_dung, ngay_dang, nguoi_dang, trang_thai, so_luong, uu_tien, loai_tin', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tieu_de' => 'Tiêu đề:',
			'noi_dung_trich' => 'Nội dung trích:',
			'hinh_anh' => 'Hình ảnh:',
			'noi_dung' => 'Nội dung tin:',
			'ngay_dang' => 'Ngày đăng:',
			'nguoi_dang'=>'nguoi_dang',
			'trang_thai' => 'Trạng thái duyệt:',
			'so_luong' => 'Số lượt xem:',
			'uu_tien' => 'Độ ưu tiên:',
			'loai_tin' => 'Loại tin:',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tieu_de',$this->tieu_de,true);
		$criteria->compare('noi_dung_trich',$this->noi_dung_trich,true);
		$criteria->compare('hinh_anh',$this->hinh_anh,true);
		$criteria->compare('noi_dung',$this->noi_dung,true);
		$criteria->compare('ngay_dang',$this->ngay_dang,true);
		$criteria->compare('trang_thai',$this->trang_thai);
		$criteria->compare('so_luong',$this->so_luong);
		$criteria->compare('uu_tien',$this->uu_tien);
		$criteria->compare('loai_tin',$this->loai_tin);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function selectUuTien(){
		$criteria=new CDbCriteria;
		$criteria->condition='trang_thai=1';
		$criteria->order='id desc';
		$criteria->limit=5 ;
		$result = $this->findAll($criteria);
		return $result;
	}
	public function selectTopList()
	{
		$criteria=new CDbCriteria;
		$criteria->condition='trang_thai=1';
		$criteria->order='id desc';
		$criteria->limit=10 ;
		$result = $this->findAll($criteria);
		return $result;
	}
	
	public function selectTP($loai)
	{
		$criteria=new CDbCriteria;
		$criteria->condition='loai_tin='.$loai.' and trang_thai=1';
		$criteria->order='id desc';
		$criteria->limit= 1;
		$result = $this->findAll($criteria);
		return $result;

	}
	public function selectListTP($loai, $id, $num)
	{
		$criteria=new CDbCriteria;
		$criteria->condition='loai_tin='.$loai.' and trang_thai=1 and id <>'.$id;
		$criteria->order='id desc';
		$criteria->limit= $num;
		$result = $this->findAll($criteria);
		return $result;

	}
	public function selectTinDocNhieu(){
		$criteria=new CDbCriteria;
		$criteria->condition='trang_thai=1';
		$criteria->order='so_luong desc';
		$criteria->limit=10 ;
		$result = $this->findAll($criteria);
		return $result;
	}
}